#include "posix_emu.h"
extern char *pwd;
/*
int fopen(char *cFileName)
{
  char *tmpdata = (char*)malloc(1024);
  if(cFileName[1]!=':')
    sprintf(tmpdata,"%s%s",pwd,cFileName);
  else
    sprintf(tmpdata,"%s",cFileName);
  log(tmpdata);
  int ret = fopenA(tmpdata,A_ReadWrite + A_Create + A_BIN + A_Append, P_READ+P_WRITE,0);
  mfree(tmpdata);
  return(ret);
}
*/
/*#include "..\inc\swilib.h"
typedef struct
{
  int handler;
  int seek;
  int size;
}PE_FILE;
PE_FILE *PE_fopen(const char *fname,char *do_not_use);
int PE_fclose(PE_FILE *pFile);
size_t PE_fread ( void * ptr, size_t size, size_t count, PE_FILE * pFile );
int PE_fseek ( PE_FILE * pFile, long int offset, int origin );
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
long int PE_ftell ( PE_FILE * pFile );*/

PE_FILE *PE_fopen(const char *fname,char *do_not_use)
{
  FSTATS stats;
/*  char *tmpdata = (char*)malloc(1024);
  if(fname[1]!=':')
    sprintf(tmpdata,"%s%s",pwd,fname);
  else
    sprintf(tmpdata,"%s",fname);
  
  mfree(tmpdata);  */
  GetFileStats(fname,&stats,0);
  PE_FILE *thisfile = (PE_FILE*)malloc(sizeof(PE_FILE));
  thisfile->handler=fopen((char*)fname, A_ReadWrite + A_Create + A_BIN + A_Append, P_READ+P_WRITE, 0);
  lseek(thisfile->handler,0,S_SET,0,0);
  thisfile->seek=0;
  thisfile->size=stats.size;
  return(thisfile);
}

int PE_fclose(PE_FILE *pFile)
{
  fclose(pFile->handler,0);
  mfree(pFile);
  return(0);
}

size_t PE_fread ( void * ptr, size_t size, size_t count, PE_FILE * pFile )
{
  if((ptr == 0)||(pFile == 0))
  {
    //ShowMSG(0,(int)"NULL POINTER EXCEPTION");
    return(0);
  }
  int readed = fread(pFile->handler, ptr, size*count,0);
  pFile->seek=pFile->seek + readed;
  return(readed);
}


int PE_fseek ( PE_FILE * pFile, long int offset, int origin )
{
  int ret = lseek(pFile->handler,offset,origin,0,0);
  if(origin == S_SET)
  {
    pFile->seek = offset;
  }
  if(origin == S_CUR)
  {
    pFile->seek += offset;
  }
  if(origin == S_END)
  {
    pFile->seek = pFile->size - offset;
  }
  
  return(0);
}
long int PE_ftell ( PE_FILE * pFile )
{
  return(pFile->seek);
}

#pragma swi_number=199
__swi void _thumb_swi0(void);

#pragma swi_number=199
__swi void _thumb_swi1(int,...);

#pragma inline=forced
void memcpy_a(void *dest,const void *source,int cnt)
{
  _thumb_swi1((int)dest,source,cnt);
  asm("DC16 0x011E");
}

#pragma diag_suppress=Pe940
#pragma inline=forced
void *memmove_a(void *dest,const void *source,int cnt)
{
  _thumb_swi1((int)dest,source,cnt);
  asm("DC16 0x0132");
}

#pragma inline=forced
void longjmp_a(jmp_buf j, int d)
{
  _thumb_swi1((int)j,d);
  asm("DC16 0x0229");
}

#pragma diag_suppress=Pe940
#pragma inline=forced
int setjmp_a(jmp_buf j)
{
  _thumb_swi1((int)j);
  asm("DC16 0x0162");
}

#pragma diag_suppress=Pe940
#pragma inline=forced
char * strstr_a(const char *s1,const char *s2)
{
  _thumb_swi1((int)s1, s2);
  asm("DC16 0x0118");
}
